package Game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.util.Random;

import Game.Effect.EffectType;

public class Politic extends Enemy
{
	public enum PoliticType
	{
		VEREADOR,
		DEPUTADO,
		SENADOR
	}
	
	float attackDelay;
	float maxAttackStrength;
	float minAttackStrength;
	float c;
	
	float paralyzeTime;
	float c2;
	
	PoliticType politicType;
	
	static Random r = new Random();
	
	int duvidas;
	
	public Politic(int x, PoliticType type, int spawnTime)
	{
		super(x, -150, (int)(70 * 1.0f), (int)(122 * 1.0f), spawnTime);
		
		this.politicType = type;
		
		minAttackStrength = 1f;
		
		switch(type)
		{
		case VEREADOR:
			attackDelay = 1f;
			maxAttackStrength = minAttackStrength + r.nextFloat();
			lifeBar = new EnergyBar(new Point2D.Double(x, y), new Point2D.Double(10, 50), 10, true);
			moving = LoadImage("/Images/politico1_walking.png");
			break;
		case DEPUTADO:
			minAttackStrength = 2f;
			attackDelay = 2f;
			maxAttackStrength = minAttackStrength + r.nextFloat() * 5;
			lifeBar = new EnergyBar(new Point2D.Double(x, y), new Point2D.Double(10, 50), 20, true);
			moving = LoadImage("/Images/politico2_walking.png");
			break;
		case SENADOR:
			minAttackStrength = 4f;
			attackDelay = 4f;
			maxAttackStrength = minAttackStrength + r.nextFloat() * 10;
			lifeBar = new EnergyBar(new Point2D.Double(x, y), new Point2D.Double(10, 50), 30, true);
			moving = LoadImage("/Images/politico3_walking.png");
			break;
		}
		
		c = attackDelay;
		
		System.out.println(type.toString() + " foi criado com " + maxAttackStrength + " de atk");
		
		float aux = 0;
		
		while ((int)aux == 0)
		{
			aux = r.nextFloat() * 3;
		}
		
		velocity = Math.min(aux, 2);
		
		animation =  new Animation(moving, 1, 4, 5, x, y, w, h);
		
		animationActive = false;
		
		active = true;
		visible = false;
		
		duvidas = Math.max(r.nextInt(8), 3);
	}
	public void Move()
	{
		state = EnemyState.MOVING;
		animation = new Animation(moving, 1, 4, 5, x, y, w, h);
		animationActive = true;
		visible = true;
	}
	public void DropProva()
	{
		ItemManager.Add(new Prova(x, y, 50, 50));
		System.out.println(this.politicType + " dropou Prova");
	}
	public void DropDuvida()
	{
		if(duvidas > 0)
		{
			ItemManager.Add(new Duvida(x + w, y, 50, 150));
			duvidas--;
		}		
	}
	public void Kill()
	{
		if(active)
		{
			state = EnemyState.DEAD;
			DropProva();
			animationActive = false;
		}
	}	
	public void Paralyze(int time)
	{
		if(state != EnemyState.PARALYZED &&
		   state != EnemyState.DEAD)
		{
			state = EnemyState.PARALYZED;
			paralyzeTime = time;
			
			System.out.println("Inimigo " + politicType.toString() + " foi paralizado por " + time + "s");
		}
	}
	public void Update()
	{
		if(this.visible)
		{
			super.Update();
			
			if(active)
			{
				if(lifeBar.CurrentEnergy <= 0)
				{
					Kill();
				}
				
				switch(state)
				{
				case DEAD:
					this.visible = false;
					this.active = false;
					break;
				case IDLE:
					 
					CollisionUpdate();
					
					break;
				case PARALYZED:
					c2+= 0.1f;
					
					if(Mouse.clicked) //CHECA COLISAO COM MOUSE PARA DROPAR DUVIDAS OU TIRAR VIDA
					{
						if(getRectangle().contains(Mouse.lastPressedPosition))
						 {
							if(duvidas > 0)
							{
								DropDuvida();
							}
							lifeBar.CurrentEnergy --;
							System.out.println("Mouse colidiu em " + politicType.toString());
						 }
					}
					if(c2 >= paralyzeTime)
					{
						boolean a = false;
						
						for(Money m : MoneyManager.money)
						{
							if(getRectangle().intersects(m.getRectangle()))
							{
								a = true;
							}
						}
						
						if(a)
						{
							state = EnemyState.ATTACKING;
						}
						else
						{
							Move();
						}
							
						c2 = 0;				
					}
					
					for(Effect e : EffectManager.effects)
					{
						if(e.type == EffectType.PROTESTO)
						{
							if(this.getRectangle().intersects(e.imgEffect.getRectangle()))
							{
								this.y -= velocity;
							}
						}
					}
					
					break;
				case ATTACKING:
					
					Behaviour();
					
					boolean collidingMoney = false;
					
					for(Money m : MoneyManager.money)
					{
						if(this.getRectangle().intersects(m.getRectangle()))
						{
							collidingMoney = true;
						}
					}
					if(collidingMoney) // SE ESTIVER COLIDINDO COM MONEYs
					{
						AttackMoney();
						
						if(Mouse.clicked) //CHECA COLISAO COM MOUSE PARA DROPAR DUVIDAS OU EMPURRAR INIMIGO
						{
							if(getRectangle().contains(Mouse.lastClickedPosition))
							 {
								if(duvidas > 0)
								{
									DropDuvida();
								}
								y --;
								System.out.println("Mouse colidiu em " + politicType.toString());
							 }
						}
					}					
					else
					{
						AttackAliados();
						
						if(Mouse.clicked) //CHECA COLISAO COM MOUSE PARA DROPAR DUVIDAS OU TIRAR VIDA
						{
							if(getRectangle().contains(Mouse.lastClickedPosition))
							 {
								if(duvidas > 0)
								{
									DropDuvida();
								}
								lifeBar.CurrentEnergy --;
								System.out.println("Mouse colidiu em " + politicType.toString());
							 }
						}
					}
					
					break;
				case MOVING:
					
					Behaviour();
					
					y += velocity;
					
					CollisionUpdate();
					
					break;
				}
				lifeBar.UpdatePosition(this.getRectangle());
			}
		}
	}
	
	public void Draw(Graphics2D g2d)
	{		
		if(visible)
		{
			super.Draw(g2d);
			Graphics2D previousGraphics = g2d;
			switch(state)
			{
				case ATTACKING:
					g2d.setColor(Color.red);
					break;
				case PARALYZED:
					g2d.setColor(Color.pink);
					g2d.drawString("" + (int)(paralyzeTime - c2), x, y - 10);
					break;
				default:
					g2d.setColor(Color.gray);
					break;
			}
			
			g2d.drawString(politicType.toString(), x, y + h + 20);
			
			g2d = previousGraphics;
			
			lifeBar.Draw(g2d, false);
		}
	}
	
	private void CollisionUpdate()
	{
		for(Money m : MoneyManager.money)
		{
			if(this.getRectangle().intersects(m.getRectangle()))
			{
				state = EnemyState.ATTACKING;
			}
		}
		
		if(AllyManager.politics != null)
		{
			for(PoliticAlly ally : AllyManager.politics)
			{
				if(this.getRectangle().intersects(ally.getRectangle()))
				{
					state = EnemyState.ATTACKING;
				}
			}
		}
		if(AllyManager.CPIs != null)
		{
			for(CPI ally : AllyManager.CPIs)
			{
				if(this.getRectangle().intersects(ally.getRectangle()))
				{
					state = EnemyState.ATTACKING;
				}
			}
		}
	}
	
	protected void AttackMoney()
	{
		c += 0.1f;
		
		if(c >= attackDelay)
		{
			animationActive = false;
			int attackPower = (int)Math.max(minAttackStrength, r.nextInt((int)maxAttackStrength));
			
			for(Money m : MoneyManager.money)
			{
				if(m.getRectangle().intersects(this.getRectangle()))
				{
					m.lifeBar.CurrentEnergy -= attackPower;
				}
			}
			
			//System.out.println(type.toString() + " atacou a bandeira e tirou " + attackPower + " de dano");
			c = 0;
		}
	}
	
	protected void AttackAliados()
	{
		c += 0.1f;
		
		boolean attacked = false;
		
		if(c >= attackDelay)
		{
			animationActive = false;
			if(AllyManager.politics != null)
			{
				for(PoliticAlly ally : AllyManager.politics)
				{
					if(this.getRectangle().intersects(ally.getRectangle()))
					{
						if(ally.visible)
						{
							int attackPower = (int)Math.max(minAttackStrength, r.nextInt((int)maxAttackStrength) * 2);
							
							switch(politicType)
							{
								case VEREADOR:
									
									ally.lifeBar.CurrentEnergy -= attackPower;
									
									break;
								case DEPUTADO:
									
									ally.lifeBar.CurrentEnergy -= attackPower * 2;
									
									break;
								case SENADOR:
									
									ally.lifeBar.CurrentEnergy -= attackPower * 3;
									
									break;
							}
							
							attacked = true;
							
							break;
						}
					}
				}
			}
			if(AllyManager.CPIs != null)
			{
				for(CPI ally : AllyManager.CPIs)
				{
					if(ally.visible)
					{
						if(ally.getRectangle().intersects(getRectangle()))
						{
							int attackPower = (int)Math.max(minAttackStrength, r.nextInt((int)maxAttackStrength));
							ally.lifeBar.CurrentEnergy -= attackPower;
							attacked = true;
							break;
						}
					}
				}
			}
			else
			{
				Move();
			}
			
			if(attacked == false)
			{
				Move();
			}
			c = 0;
		}
	}
	
	protected void Behaviour()
	{
		//Comportamento do inimigo
		switch(politicType)
		{
			case VEREADOR:
				
				break;
			case DEPUTADO:
				if(r.nextInt(50) == 1)
				{
					for(int i = 0; i < Level.caminho.gridRects.length; i++)
					{
						boolean b = false;
						
						for(int j = 0; j < Level.caminho.gridRects[i].length; j++)
						{
							if(this.getRectangle().intersects(Level.caminho.gridRects[i][j]))
							{
								int a;
								int d;
								int u;
								boolean move = true;
								
								if(r.nextInt(2) == 1) //direita
								{
									a  = j + 1 < Level.caminho.gridColumns ? j + 1 : 0;
									d = i + 1 < Level.caminho.gridRows ? i + 1 : i;
									u = i - 1 > 0 ? i - 1 : i;
								}									
								else //esquerda
								{
									a  = j > 0 ? j - 1 : Level.caminho.gridColumns - 1;
									d = i + 1 < Level.caminho.gridRows ? i + 1 : i;
									u = i - 1 > 0 ? i - 1 : i;
								}
								
								for(Carta c : CartaManager.cartas)
								{
									if(c.getRectangle().intersects(Level.caminho.gridRects[i][a]))
										move = false;
									if(c.getRectangle().intersects(Level.caminho.gridRects[d][a]))
										move = false;
									if(c.getRectangle().intersects(Level.caminho.gridRects[u][a]))
										move = false;
								}
								
								for(Enemy e : EnemyManager.inimigos)
								{
									if(e.getRectangle().intersects(Level.caminho.gridRects[i][a]))
										move = false;
									if(e.getRectangle().intersects(Level.caminho.gridRects[d][a]))
										move = false;
									if(e.getRectangle().intersects(Level.caminho.gridRects[u][a]))
										move = false;
								}
								
								for(PoliticAlly ally : AllyManager.politics)
								{
									if(ally.getRectangle().intersects(Level.caminho.gridRects[i][a]))
										move = false;
									if(ally.getRectangle().intersects(Level.caminho.gridRects[d][a]))
										move = false;
									if(ally.getRectangle().intersects(Level.caminho.gridRects[u][a]))
										move = false;
								}
								for(CPI ally : AllyManager.CPIs)
								{
									if(ally.getRectangle().intersects(Level.caminho.gridRects[i][a]))
										move = false;
									if(ally.getRectangle().intersects(Level.caminho.gridRects[d][a]))
										move = false;
									if(ally.getRectangle().intersects(Level.caminho.gridRects[u][a]))
										move = false;
								}
								
								if(move)
									x = (int)Level.caminho.gridRects[i][a].getCenterX() - w/2;
								
								b = true;
								break;
							}
						}
						if(b)
							break;
					}
				}
				break;
			case SENADOR:
				if(r.nextInt(20) == 1)
				{
					for(int i = 0; i < Level.caminho.gridRects.length; i++)
					{
						boolean b = false;
						
						for(int j = 0; j < Level.caminho.gridRects[i].length; j++)
						{
							if(this.getRectangle().intersects(Level.caminho.gridRects[i][j]))
							{
								int a;
								int d;
								int u;
								boolean move = true;
								
								if(r.nextInt(2) == 1) //direita
								{
									a  = j + 1 < Level.caminho.gridColumns ? j + 1 : 0;
									d = i + 1 < Level.caminho.gridRows ? i + 1 : i;
									u = i - 1 > 0 ? i - 1 : i;
								}
									
								else //esquerda
								{
									a  = j > 0 ? j - 1 : Level.caminho.gridColumns - 1;
									d = i + 1 < Level.caminho.gridRows ? i + 1 : i;
									u = i - 1 > 0 ? i - 1 : i;
								}
								
								for(Carta c : CartaManager.cartas)
								{
									if(c.getRectangle().intersects(Level.caminho.gridRects[i][a]))
										move = false;
									if(c.getRectangle().intersects(Level.caminho.gridRects[d][a]))
										move = false;
									if(c.getRectangle().intersects(Level.caminho.gridRects[u][a]))
										move = false;
								}
								for(Enemy e : EnemyManager.inimigos)
								{
									if(e.getRectangle().intersects(Level.caminho.gridRects[i][a]))
										move = false;
									if(e.getRectangle().intersects(Level.caminho.gridRects[d][a]))
										move = false;
									if(e.getRectangle().intersects(Level.caminho.gridRects[u][a]))
										move = false;
								}
								
								for(PoliticAlly ally : AllyManager.politics)
								{
									if(ally.getRectangle().intersects(Level.caminho.gridRects[i][a]))
										move = false;
									if(ally.getRectangle().intersects(Level.caminho.gridRects[d][a]))
										move = false;
									if(ally.getRectangle().intersects(Level.caminho.gridRects[u][a]))
										move = false;
								}
								
								for(CPI ally : AllyManager.CPIs)
								{
									if(ally.getRectangle().intersects(Level.caminho.gridRects[i][a]))
										move = false;
									if(ally.getRectangle().intersects(Level.caminho.gridRects[d][a]))
										move = false;
									if(ally.getRectangle().intersects(Level.caminho.gridRects[u][a]))
										move = false;
								}
								
								if(move)
									x = (int)Level.caminho.gridRects[i][a].getCenterX() - w/2;
								
								b = true;
								break;
							}
						}
						
						if(b)
							break;
					}
				}
				break;
		}
	}
	@Override
	public void Slow() 
	{
		float aux = r.nextFloat();
		
		while(velocity - aux <= 0)
		{
			aux -= 0.1f;
		}
		
		if((int)(velocity - aux) > 0)
			velocity -= aux;
		
		attackDelay += r.nextInt(2);
	}
}
